package com.haochang.chunkvod.view.vod.ViewGroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.haochang.chunkvod.R;

/**
 * 优惠券view
 *     @author Yangjie
 *     className CouponView
 *     created at  2016/11/9  15:19
 */
public class CouponView extends LinearLayout
{
	private Paint mPaint;
	private float leftRadius = 10;//左边半圆半径
	private float rightRadius = 10;//右边半圆半径
	private float corners = 10;//四个角的半径
	private int mSawtoothColor;//锯齿部分背景颜色
	private int mBackgroundColor;//整体背景颜色，即半圆和锯齿的填充色

	private RectF leftRectF, rightRectF, rectF;

	public CouponView(Context context)
	{
		this(context, null);
	}

	public CouponView(Context context, AttributeSet attrs)
	{
		this(context, attrs, 0);
	}

	public CouponView(Context context, AttributeSet attrs, int defStyleAttr)
	{
		super(context, attrs, defStyleAttr);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CouponView);

		rightRadius = typedArray.getDimension(R.styleable.CouponView_rightRadius, 10);
		leftRadius = typedArray.getDimension(R.styleable.CouponView_leftRadius, 10);
		corners = typedArray.getDimension(R.styleable.CouponView_corners, 10);
		mSawtoothColor = typedArray.getColor(R.styleable.CouponView_sawtooth_color, 0);
		mBackgroundColor = typedArray.getColor(R.styleable.CouponView_background_color, 0);
		typedArray.recycle();
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setDither(true);//防抖动
		mPaint.setStyle(Paint.Style.FILL);

		leftRectF = new RectF();
		rightRectF = new RectF();
		rectF = new RectF();
		setWillNotDraw(false);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		float width = getWidth() - getPaddingLeft() - getPaddingRight();
		float height = getHeight() - getPaddingTop() - getPaddingBottom();
		mPaint.setColor(mSawtoothColor);
		//画锯齿背景
		rectF.set(getPaddingLeft(), getPaddingTop(), getPaddingLeft() + (width * 1), getPaddingTop() + height);
		canvas.drawRoundRect(rectF, corners, corners, mPaint);
		mPaint.setColor(mBackgroundColor);
		//画其余部分
		canvas.drawRect(getPaddingLeft() + width * 1, getPaddingTop(), width + getPaddingLeft(), height + getPaddingTop(), mPaint);
		//画左边的半圆
		leftRectF.set(getPaddingLeft() - leftRadius, getPaddingTop() + (height - 2 * leftRadius) / 2, getPaddingLeft() + leftRadius,
				getPaddingTop() + (height - 2 * leftRadius) / 2 + 2 * leftRadius);
		canvas.drawArc(leftRectF, -90, 180, true, mPaint);
		//画右边的半圆
		rightRectF.set(getWidth() - getPaddingLeft() - rightRadius, getPaddingTop() + (height - 2 * rightRadius) / 2,
				getWidth() - getPaddingLeft() + (float) Math.ceil(rightRadius), getPaddingTop() + (height - 2 * rightRadius) / 2 + 2 * rightRadius);
		canvas.drawArc(rightRectF, 90, 180, true, mPaint);
	}

	/**
	 * 设置整体背景颜色
	 * @param color 背景色
	 */
	public void setBackgroundColor_(int color)
	{
		mSawtoothColor = color;
		invalidate();
	}
}
